/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
        document.getElementById("test").addEventListener("click",test);
        document.getElementById("conncetdevice").addEventListener("click",conncetdevice);
        document.getElementById("setname").addEventListener("click",setname);
        document.getElementById("disconnectDevice").addEventListener("click",disconnectDevice);
        document.getElementById("readbankcardinfo").addEventListener("click",readbankcardinfo);
        document.getElementById("iscondevice").addEventListener("click",iscondevice);
        document.getElementById("cancelOperation").addEventListener("click",cancelOperation);
        document.getElementById("readICCardARQC").addEventListener("click",readICCardARQC);
        document.getElementById("getPin").addEventListener("click",getPin);
//        document.getElementById("getPublicKey").addEventListener("click",getPublicKey);
        document.getElementById("loadMainKey").addEventListener("click",loadMainKey);
        document.getElementById("loadWorkKey").addEventListener("click",loadWorkKey);
        document.getElementById("readFinger").addEventListener("click",readFinger);
        document.getElementById("openSignature").addEventListener("click",openSignature);
        document.getElementById("getSignaturePhoto").addEventListener("click",getSignaturePhoto);
        document.getElementById("clearSignature").addEventListener("click",clearSignature);
        document.getElementById("closeSignature").addEventListener("click",closeSignature);
        document.getElementById("readIDCardInfo").addEventListener("click",readIDCardInfo);
//        document.getElementById("takecamera").addEventListener("click",takecamera);

    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};



//成功的返回事件
function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
    console.log("Camera cleanup success."+image);
}
//失败的返回事件
function onFail(message) {
console.log("Camera cleanup onFail."+message);
}

function test(){
console.log("Camera cleanup success.")

Device.initDevice();

};

function conncetdevice(){
console.log("Camera cleanup success.")

Device.conncetDevice();

};


function setname(){
console.log("Camera cleanup success.")

Device.setDeviceName("id001");

};

function disconnectDevice(){
console.log("Camera cleanup success.")

Device.disConnectDevice();

};

function readbankcardinfo(){
console.log("Camera cleanup success.")

Device.readBankCardInfo();

};

function iscondevice(){
console.log("Camera cleanup success.")

Device.isConnDevice();

};


function cancelOperation(){
console.log("Camera cleanup success.")

Device.cancelOperation();

};

function readICCardARQC(){
console.log("Camera cleanup success.")

Device.readICCardARQC();

};

function getPin(){
console.log("Camera cleanup success.")

Device.getPin();

};

function getPublicKey(){
console.log("Camera cleanup success.")

Device.getPublicKey();

};
function loadMainKey(){
console.log("Camera cleanup success.")

Device.loadMainKey();

};
function loadWorkKey(){
console.log("Camera cleanup success.")

Device.loadWorkKey();

};

function readFinger(){
console.log("Camera cleanup success.")

Device.readFinger();

};
function openSignature(){
console.log("Camera cleanup success.")

Device.openSignature();

};
function getSignaturePhoto(){
console.log("Camera cleanup success.")

Device.getSignaturePhoto();

};
function clearSignature(){
console.log("Camera cleanup success.")

Device.clearSignature();

};
function closeSignature(){
console.log("Camera cleanup success.")

Device.closeSignature();

};

function readIDCardInfo(){

console.log("readIDCardInfo.");

Device.readIDCardInfo(
(res)=>{

 console.log("readIDCardInfo",res);
// alert("readIDCardInfo"+res);
}


);
//console.log("log","aaa"+Device.readIDCardInfo());


};


function takecamera(){
console.log("Camera cleanup success.")

navigator.camera.getPicture(onSuccess,onFail,{
      quality: 100,
      targetWidth:100,
      targetHeight:100,
      destinationType: Camera.DestinationType.DATA_URL});
//CameraLauncher.takePicture();
};

app.initialize();